package com.xsq.audio.player.fragment

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.Bitmap
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.LinearInterpolator
import androidx.fragment.app.Fragment
import com.xsq.audio.databinding.FragPlayerCoverviewBinding
import com.xsq.audio.player.PlayManager
import com.xsq.audio.player.utils.LogUtil

class CoverFragment : Fragment() {

    val TAG = "CoverFragment"

    //当前专辑图片
    var currentBitmap: Bitmap? = null

    //是否初始化，第一次进入界面不播放切换动画
    var isInitAnimator = false

    //旋转属性动画
    private var coverAnimator: ObjectAnimator? = null
    private var objectAnimator1: ObjectAnimator? = null
    private var objectAnimator3: ObjectAnimator? = null
    private var objectAnimator2: ObjectAnimator? = null
    private var animatorSet: AnimatorSet? = null
    private var clickListener: (() -> Unit)? = null
    private lateinit var binding: FragPlayerCoverviewBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragPlayerCoverviewBinding.inflate(inflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    private fun initViews() {
        binding.cover2View.setOnClickListener {
            clickListener?.invoke()
        }
        binding.coverView.setOnClickListener {
            clickListener?.invoke()
        }
        initAlbumPic()
    }

    /**
     * 设置Bitmap
     */
    fun setImageBitmap(bm: Bitmap?) {
        binding.coverView.setImageBitmap(bm)
        binding.coverView.visibility = View.VISIBLE
        if (currentBitmap == null) {
            binding.cover2View.visibility = View.GONE
            binding.cover2View.setImageBitmap(bm)
        }
        currentBitmap = bm
    }

    /**
     * 初始化旋转动画
     */
    private fun initAlbumPic() {
        //旋转动画
        coverAnimator = ObjectAnimator.ofFloat(binding.cover2View, "rotation", 0F, 359F).apply {
            duration = (20 * 1000).toLong()
            repeatCount = -1
            repeatMode = ObjectAnimator.RESTART
            interpolator = LinearInterpolator()
            addUpdateListener {
                //同时更新civ_cover_2
                binding.coverView.rotation = it.animatedValue as Float
                binding.cover2View.rotation = it.animatedValue as Float
            }
        }
        //缩放动画
        objectAnimator1 = ObjectAnimator.ofFloat(binding.cover2View, "scaleX", 1f, 0.7f).apply {
            duration = 500L
            interpolator = AccelerateInterpolator()
            interpolator = AccelerateInterpolator()
            addUpdateListener {
                binding.cover2View.scaleY = it.animatedValue as Float
                binding.cover2View.scaleX = it.animatedValue as Float
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationEnd(animation: Animator) {
                    LogUtil.d("objectAnimator", "objectAnimator1 onAnimationEnd")
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationStart(animation: Animator) {
                    LogUtil.d("objectAnimator", "objectAnimator1 onAnimationStart")
                    //开始时，初始化状态
                    binding.coverView.alpha = 0f
                    binding.cover2View.translationY = 0f
                    binding.cover2View.visibility = View.VISIBLE
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })
        }
        //coverView 由透明变成不透明
        objectAnimator3 = ObjectAnimator.ofFloat(binding.coverView, "alpha", 0f, 1F).apply {
            duration = 300L
            addUpdateListener {
                binding.coverView.alpha = it.animatedValue as Float
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationEnd(animation: Animator) {
                    binding.coverView.alpha = 1f
                    LogUtil.d("objectAnimator", "objectAnimator3 onAnimationEnd")
                }

                override fun onAnimationCancel(animation: Animator) {
                    binding.coverView.alpha = 1f
                }

                override fun onAnimationStart(animation: Animator) {
                    LogUtil.d("objectAnimator", "objectAnimator3 动画开始")
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })
        }
        //civ_cover_2 上移动画
        objectAnimator2 = ObjectAnimator.ofFloat(binding.cover2View, "translationY", 0f, -1000f).apply {
            duration = 300L
            interpolator = AccelerateInterpolator()
            addUpdateListener {
                binding.cover2View.translationY = it.animatedValue as Float
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationEnd(animation: Animator) {
                    binding.cover2View.translationY = 0f
                    binding.cover2View.setImageBitmap(currentBitmap)
                    binding.cover2View.visibility = View.GONE
                    LogUtil.d("objectAnimator", "objectAnimator2 onAnimationEnd")
                }

                override fun onAnimationCancel(animation: Animator) {
                    binding.coverView.alpha = 1f
                }

                override fun onAnimationStart(animation: Animator) {
                    LogUtil.d("objectAnimator", "objectAnimator2 动画开始")
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })
        }
        animatorSet = AnimatorSet()
        animatorSet?.play(objectAnimator2)?.with(objectAnimator3)?.after(objectAnimator1)
    }

    /**
     * 切换歌曲，开始旋转动画
     */
    fun startRotateAnimation(isPlaying: Boolean = false) {
        LogUtil.d(TAG, "startRotateAnimation ，isInitAnimator=$isInitAnimator isPlaying =$isPlaying")
        if (isPlaying) {
            coverAnimator?.pause()
            coverAnimator?.start()
        }
        if (isInitAnimator) {
            binding.cover2View.visibility = View.VISIBLE
            //组合动画
            animatorSet?.pause()
            animatorSet?.start()
        } else {
            //第一次进入不播放切换动画
            isInitAnimator = true
            binding.cover2View.visibility = View.GONE
        }
    }

    /**
     * 停止旋转
     */
    fun stopRotateAnimation() {
        coverAnimator?.pause()
    }

    /**
     * 继续旋转
     */
    fun resumeRotateAnimation() {
        coverAnimator?.isStarted?.let {
            if (it) coverAnimator?.resume() else coverAnimator?.start()
        }
    }

    override fun onResume() {
        super.onResume()
        if (coverAnimator != null && coverAnimator?.isPaused!! && PlayManager.isPlaying()) {
            coverAnimator?.resume()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        coverAnimator?.cancel()
        coverAnimator = null
    }

    override fun onStop() {
        super.onStop()
        coverAnimator?.pause()
        animatorSet?.pause()
    }

    fun setOnclickAlbumListener(clickListener: (() -> Unit)?) {
        this.clickListener = clickListener;
    }
}